import mysql from 'mysql2'

import { MYSQL_HOST, MYSQL_PORT, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD } from './config'

const connections = mysql.createPool({
  host: MYSQL_HOST,
  port: MYSQL_PORT,
  database: MYSQL_DATABASE,
  user: MYSQL_USER,
  password: MYSQL_PASSWORD,
})

connections.getConnection((err, conn) => {
  if (err) return console.log('mysql连接发生致命错误:', err)
  conn.connect((err) => {
    if (err) {
      console.log('mysql连接失败:', err)
    } else {
      console.log('mysql连接成功~')
    }
  })
})

export default connections.promise()
